<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\ListBuilder;
use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageUserAddress;
use support\Request;

class UserAddressController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addScreen('uid', 'input', 'UID');
        $builder->addScreen('name', 'input', '姓名');
        $builder->addScreen('mobile', 'input', '手机号');
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumnEle('headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('nickname', '昵称')
            ->addColumn('province', '省份')
            ->addColumn('city', '城市')
            ->addColumn('area', '区域')
            ->addColumn('street', '街道')
            ->addColumn('address_detail', '详细地址')
            ->addColumn('name', '姓名')
            ->addColumn('mobile', '手机号')
            ->addColumnEle('is_default', '默认', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => [
                        0 => '否',
                        1 => '是'
                    ],
                    'style' => [
                        0 => [
                            'type' => 'info'
                        ],
                        1 => [
                            'type' => 'success'
                        ]
                    ]
                ],
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['u.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if ($uid) {
            $where[] = ['a.uid', '=', $uid];
        }
        $name = $request->get('name');
        if ($name) {
            $where[] = ['a.name', 'like', "%$name%"];
        }
        $mobile = $request->get('mobile');
        if ($mobile) {
            $where[] = ['a.mobile', 'like', "%$mobile%"];
        }
        $data = MassageUserAddress::where($where)
            ->alias('a')
            ->join('massage_user u', 'a.uid=u.id')
            ->order('id desc')
            ->field('a.*,u.nickname,u.headimg')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->nickname = base64_decode($item->nickname);
                if (strpos($item->headimg, 'http') !== 0) {
                    $item->headimg = UploadService::url($item->headimg);
                }
            });
        return $this->successRes($data);
    }
}
